package com.juyu.miliao.scheduler.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.common.domain.SanGuoGameRankingList;
import com.juyu.miliao.common.util.DateUtil;
import com.juyu.miliao.scheduler.dao.mysql.AppkeyDao;
import com.juyu.miliao.scheduler.dao.mysql.SanGuoGameRankingListDao;
import com.juyu.miliao.scheduler.dao.redis.BeansGameRedisDao;
import com.juyu.miliao.scheduler.domin.Appkey;
import com.juyu.miliao.common.domain.BeansGameDTO;
import com.juyu.miliao.scheduler.service.GameRankingListService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 *
 */
@Service("SanGuoGameRankingListServiceImpl")
public class SanGuoGameRankingListServiceImpl implements GameRankingListService {
    private static final String TIME_KEY = "coolSummerActivities";
    @Resource
    private BeansGameRedisDao beansGameRedisDao;
    @Resource
    private SanGuoGameRankingListDao sanGuoGameRankingListDao;
    @Resource
    private AppkeyDao appkeyDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setRankingList(BeansGameDTO beansGameDTO) {

        if (!StringUtils.isEmpty(beansGameDTO.getProfit())&&beansGameDTO.getProfit()>0) {

            DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

            LocalDate sanGuoProfitDate = LocalDate.parse(beansGameDTO.getCreateDate(), dateFormatter);

            Appkey appkey = appkeyDao.getAppkeyByKey(TIME_KEY);
            JSONObject json = JSONObject.parseObject(appkey.getValue());

            String startDateTarget = json.getString("startDate");

            String endDateTarget = json.getString("endDate");

            LocalDate startDate = LocalDate.parse(startDateTarget,
                    DateTimeFormatter.ofPattern("yyyy-MM-dd"));

            LocalDate endDate = LocalDate.parse(endDateTarget,
                    DateTimeFormatter.ofPattern("yyyy-MM-dd"));

            if (DateUtil.checkBetweenTime(startDate, endDate, sanGuoProfitDate)) {
                SanGuoGameRankingList sanGuoGameRankingList = new SanGuoGameRankingList();
                sanGuoGameRankingList.setUserId(beansGameDTO.getUserId());
                sanGuoGameRankingList.setWinsBeans(beansGameDTO.getProfit());
                SanGuoGameRankingList winBeansGameSort1 = sanGuoGameRankingListDao.selectWinBeansByUserId(beansGameDTO.getUserId());
                int i = 0;
                if (winBeansGameSort1 == null) {
                    i = sanGuoGameRankingListDao.saveWinBeansForSanGuo(sanGuoGameRankingList);

                } else {
                    i = sanGuoGameRankingListDao.updateWinBeansForSanGuo(sanGuoGameRankingList);
                }

                if (i > 0) {
                    beansGameRedisDao.setSanGuoGameWinBeans(LocalDate.now(), beansGameDTO.getUserId(),
                            beansGameDTO.getProfit());
                }
            }
        }
    }
}
